﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Bluetooth_OnOffer
{
  public partial class MainForm : Form
  {
    #region BthGetMode and BthSetMode import
    [DllImport("BthUtil.dll", SetLastError = true)]
    public static extern int BthSetMode(RadioMode dwMode);

    [DllImport("BthUtil.dll", SetLastError = true)]
    public static extern int BthGetMode(out RadioMode dwMode);
    #endregion

    public enum RadioMode
    {
      Off          = 0,
      Connectable  = 1,
      Discoverable = 2,
    }

    public MainForm()
    {
      InitializeComponent();

      RadioMode mode;
      BthGetMode(out mode);

      if (mode != RadioMode.Off)
      {
        labelMode.Text = "Bluetooth is On";
      }
      else
      {
        labelMode.Text = "Bluetooth is Off";
      }
    }

    private void buttonBluetoothSwitch_Click(object sender, EventArgs e)
    {
      RadioMode mode;
      BthGetMode(out mode);

      if (mode != RadioMode.Off)
      {
        BthSetMode(RadioMode.Off);
        labelMode.Text = "Bluetooth is Off";
      }
      else
      {
        BthSetMode(RadioMode.Connectable);
        labelMode.Text = "Bluetooth is On";
      }
    }
  }
}